package com.mass.core.sys.dict.services.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import com.mass.core.sys.dict.dao.SysDictMapper;
import com.mass.core.sys.dict.model.SysDict;
import com.mass.core.sys.dict.model.SysDictExample;
import com.mass.core.sys.dict.services.SysDictServices;
import com.mass.core.utils.AjaxResponse;

@Service
public class SysDictServicesImpl implements SysDictServices{

	@Autowired
	private SysDictMapper dictMapper;
	
	public AjaxResponse selectPage(Integer page, Integer limit, SysDict sysDict) {
		SysDictExample dictExample = new SysDictExample();
		if(!ObjectUtils.isEmpty(page)){
			dictExample.setPageIndex((page-1)*limit);
			dictExample.setPageSize(limit);
		}
		SysDictExample.Criteria criteria = dictExample.createCriteria();
		criteria.andDelFlagEqualTo(0);
		if(!ObjectUtils.isEmpty(sysDict)){
			if(!ObjectUtils.isEmpty(sysDict.getParentCode())){
				criteria.andParentCodeEqualTo(sysDict.getParentCode());
			}
		}
		List<SysDict> sysDicts = dictMapper.selectByExample(dictExample);
		long count = dictMapper.countByExample(dictExample);
		return AjaxResponse.success("ok", sysDicts, count, page);
	}
	@Transactional
	public int insert(SysDict sysDict) {
		if(!validationTypeCode(sysDict.getTypeCode(),null,null)){
			return dictMapper.insert(sysDict);
		}else{
			return 0;
		}
	}
	@Transactional
	public int update(SysDict sysDict) {
		if(!validationTypeCode(sysDict.getTypeCode(),null, sysDict.getId())){
			return dictMapper.updateByPrimaryKeySelective(sysDict);
		}else{
			return 0;
		}
	}
	@Transactional
	public int delete(Long id) {
		SysDict sysDict = new SysDict();
		sysDict.setDelFlag(1);
		sysDict.setId(id);
		return dictMapper.updateByPrimaryKeySelective(sysDict);
	}

	public List<SysDict> selectByTypeCode(String typeCode) {
		SysDictExample dictExample = new SysDictExample();
		SysDictExample.Criteria criteria = dictExample.createCriteria();
		criteria.andDelFlagEqualTo(0);
		criteria.andParentCodeEqualTo(typeCode);
		return dictMapper.selectByExample(dictExample);
	}

	public boolean validationTypeCode(String typeCode,String typeName,Long id){
		SysDictExample dictExample = new SysDictExample();
		SysDictExample.Criteria criteria = dictExample.createCriteria();
		criteria.andDelFlagEqualTo(0);
		if(!ObjectUtils.isEmpty(typeCode)){
			criteria.andTypeCodeEqualTo(typeCode.trim());
		}
		if(!ObjectUtils.isEmpty(typeName)){
			criteria.andTypeNameEqualTo(typeName.trim());
		}
		
		if(!ObjectUtils.isEmpty(id)){
			criteria.andIdNotEqualTo(id);
		}
		List<SysDict> dicts = dictMapper.selectByExample(dictExample);
		if(!ObjectUtils.isEmpty(dicts)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 多个用，隔开
	 */
	public Map<String, List<SysDict>> selectByTypeCodes(String typeCodes) {
		SysDictExample dictExample = new SysDictExample();
		String [] strs = typeCodes.split(",");
		for(String str:strs){
			SysDictExample.Criteria criteria = dictExample.createCriteria();
			criteria.andDelFlagEqualTo(0);
			criteria.andTypeCodeEqualTo(str);
			dictExample.or(criteria);
		}
		List<SysDict> dicts = dictMapper.selectByExample(dictExample);
		Map<String, List<SysDict>> map = new HashMap<String, List<SysDict>>();
		for(SysDict dict:dicts){
			map.put(dict.getTypeCode(), dict.getSysDictList());
		}
		return map;
	}
}
